package com.mycompany.oj.aspect;

import com.mycompany.oj.util.NoLoginException;
import com.mycompany.oj.util.SharedService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wucy
 */
@Aspect
@Component
public class Login {


    private SharedService sharedService = new SharedService();

    @Pointcut("@annotation(com.mycompany.oj.aspect.JudgeLog)")
    public void webLog() {
    }

    @Around("webLog()")
    public Object doBefore(ProceedingJoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("Authorization");
        if (token == null || sharedService.judgeAdmin(token)) {
            System.err.println("--------------------------抛出到异常�--------------------------------");
            throw new NoLoginException("No Login!");

        }
        return joinPoint.proceed();
    }
}
